import React, { useState } from 'react';
import { Tooltip, Checkbox, message } from 'antd';
import ImportMent from './component';
import { randomKey } from '../../../../utils/utils';
import services from '../../../../services/api';
export default function(props) {
  const [insideData, setInsideData] = useState([]); // 三方名单数据
  const [tableData, setTableData] = useState([]); // 冲突table数据
  const [evenData, setEvenData] = useState({}); // 事件
  const [appData, setAppData] = useState({}); // 渠道
  const [productData, setProductData] = useState({}); // 产品
  const [fileId, setFileId] = useState('');
  const [existedEvent, setExistedEvent] = useState(false);
  const [isImport, setIsImport] = useState(false); // 是否全选导入为准
  const [isSterm, setIsSterm] = useState(false); // 是否全选系统为准
  const coumsList = [
    {
      title: '项目',
      key: 'title',
      dataIndex: 'title',
      render: (text, record) => {
        return (
          <Tooltip title={text} placement='topLeft'>
            {text}
          </Tooltip>
        );
      },
    },
    {
      title: '描述',
      key: 'code',
      dataIndex: 'code',
      width: 250,
      render: (text, record) => {
        return (
          <Tooltip title={text} placement='topLeft'>
            {text}
          </Tooltip>
        );
      },
    },
    {
      title: '当前使用情况',
      key: 'is_used',
      dataIndex: 'is_used',
      render: (text, record) => {
        return (
          <Tooltip title={text ? '使用中' : '未被引用'} placement='topLeft'>
            {text ? '使用中' : '未被引用'}
          </Tooltip>
        );
      },
    },
    {
      title: (
        <Checkbox onChange={importCheckAll} checked={isImport}>
          全选
        </Checkbox>
      ),
      key: 'is_import',
      dataIndex: 'is_import',
      render: (text, record) => {
        return (
          <Checkbox value={record.id} onChange={checkImport} checked={text} disabled={record.type === 'event'}>
            以导入数据为准
          </Checkbox>
        );
      },
    },
    {
      title: (
        <Checkbox onChange={stremCheckAll} checked={isSterm}>
          全选
        </Checkbox>
      ),
      key: 'is_Sterm',
      dataIndex: 'is_Sterm',
      render: (text, record) => {
        return (
          <Checkbox value={record.id} onChange={checkStrem} checked={text} disabled={record.type === 'event'}>
            以系统数据为准
          </Checkbox>
        );
      },
    },
  ];
  const insideList = [
    {
      title: 'ID',
      width: 100,
      render: (text, record, index) => {
        return <>{index + 1}</>;
      },
    },
    {
      title: '项目',
      key: 'title',
      dataIndex: 'title',
      render: (text, record) => {
        return (
          <Tooltip title={text} placement='topLeft'>
            {text}
          </Tooltip>
        );
      },
    },
    {
      title: '描述',
      key: 'remark',
      dataIndex: 'remark',
      width: 250,
      render: (text, record) => {
        return (
          <Tooltip title={text} placement='topLeft'>
            {text}
          </Tooltip>
        );
      },
    },
    {
      title: '操作建议',
      key: 'now_kos',
      dataIndex: 'strategy_name',
      render: (text, record) => {
        return (
          <Tooltip title={'请先初始化数据，再导入'} placement='topLeft'>
            {'请先初始化数据，再导入'}
          </Tooltip>
        );
      },
    },
  ];
  // 选择渠道再次冲突验证
  function selectApp(value) {
    if (fileId) {
      services.strategyComponent.conflictfField({ app_id: value, file_id: fileId }).then((res) => {
        if (!res.code) {
          setTableData([]);
          res.data.uninitialized = res.data.uninitialized || [];
          res.data.app = appData;
          res.data.event = evenData;
          res.data.product = productData;
          res.data.fileId = fileId;
          if (existedEvent === res.data.existedEvent) {
            setExistedEvent(false);
          }
          setExistedEvent(res.data.existedEvent);
          // setIsImport(false);
          // setIsSterm(false);
          setDataFn(res.data);
        }
      });
    }
  }
  function setDataFn(props) {
    setIsImport(false);
    setIsSterm(false);
    if (!Object.keys(props).length) {
      setTableData([]);
      setInsideData([]);
      setEvenData({});
      setAppData({});
      setFileId('');
      // setIsImport(false);
      // setIsSterm(false);
      setExistedEvent(false);
      setProductData({});
      return;
    }
    const lists = props.conflict.map((item) => {
      item.is_import = false;
      item.is_Sterm = (item.type === 'event') ? true : false;
      item.id = randomKey();
      item.children &&
        item.children.forEach((it) => {
          it.is_import = false;
          it.is_Sterm = false;
          item.id = randomKey();
        });
      return item;
    });
    const newUnintial = props.uninitialized.map((item) => {
      item.id = randomKey();
      return item;
    });
    setTableData(lists);
    setInsideData(newUnintial);
    setEvenData(props.event);
    setAppData(props.app);
    setFileId(props.fileId);
    setExistedEvent(props.existedEvent);
    setProductData(props.product);
  }
  // 以导入为准全选
  function importCheckAll(e) {
    const lists = tableData.map((item) => {
      if (item.type === 'event') return item
      item.is_import = e.target.checked;
      if (e.target.checked) {
        item.is_Sterm = !e.target.checked;
      }
      item.children &&
        item.children.forEach((it) => {
          it.is_import = e.target.checked;
          if (e.target.checked) {
            it.is_Sterm = !e.target.checked;
          }
        });
      return item;
    });
    setIsImport(e.target.checked);
    if (e.target.checked) {
      setIsSterm(!e.target.checked);
    }
    setTableData(lists);
  }
  // 已系统为准
  function stremCheckAll(e) {
    const lists = tableData.map((item) => {
      if (item.type === 'event') return item
      item.is_Sterm = e.target.checked;
      if (e.target.checked) {
        item.is_import = !e.target.checked;
      }
      item.children &&
        item.children.forEach((it) => {
          it.is_Sterm = e.target.checked;
          if (e.target.checked) {
            it.is_import = !e.target.checked;
          }
        });
      return item;
    });
    setIsSterm(e.target.checked);
    if (e.target.checked) {
      setIsImport(!e.target.checked);
    }
    setTableData(lists);
  }
  // 导入系统单选
  function checkImport(e) {
    e.stopPropagation();
    let isAllImport = true;
    const lists = tableData.map((item) => {
      if (item.id === e.target.value) {
        item.is_import = e.target.checked;
        if (e.target.checked) {
          item.is_Sterm = !e.target.checked;
        }
      }
      !item.is_import && (isAllImport = false);
      item.children &&
        item.children.forEach((it) => {
          if (it.id === e.target.value) {
            it.is_import = e.target.checked;
            if (e.target.checked) {
              it.is_Sterm = !e.target.checked;
            }
          }
          !it.is_import && (isAllImport = false);
        });
      return item;
    });
    setIsImport(isAllImport);
    if (!isAllImport) {
      setIsSterm(isAllImport);
    } else {
      setIsSterm(false);
    }
    setTableData(lists);
  }
  // 导入系统单选
  function checkStrem(e) {
    e.stopPropagation();
    let isAllStrem = true;
    const lists = tableData.map((item) => {
      if (item.id === e.target.value) {
        item.is_Sterm = e.target.checked;
        if (e.target.checked) {
          item.is_import = !e.target.checked;
        }
      }
      !item.is_Sterm && (isAllStrem = false);
      item.children &&
        item.children.forEach((it) => {
          if (it.id === e.target.value) {
            it.is_Sterm = e.target.checked;
            if (e.target.checked) {
              it.is_import = !e.target.checked;
            }
          }
          !it.is_Sterm && (isAllStrem = false);
        });
      return item;
    });
    setIsSterm(isAllStrem);
    if (!isAllStrem) {
      setIsImport(isAllStrem);
    } else {
      setIsImport(false);
    }
    setTableData(lists);
  }
  // 验证规则
  function getRule() {
    const lists = tableData.map((item) => {
      if (!item.is_import && !item.is_Sterm) {
        message.warning('请选择冲突项解决方式');
        throw '错误';
      }
      item.import_execute_action = (item.is_import && 'rewrite') || 'skip';
      item.children &&
        item.children.forEach((it) => {
          if (!it.is_import && !it.is_Sterm) {
            message.warning('请选择冲突项解决方式');
            throw '请选择冲突项解决方式';
          }
          it.import_execute_action = (it.is_import && 'rewrite') || 'skip';
        });
      return item;
    });
    setTableData(lists);
  }
  return (
    <ImportMent
      {...props}
      coumsList={coumsList}
      insideList={insideList}
      insideData={insideData}
      tableData={tableData}
      evenData={evenData}
      appData={appData}
      productData={productData}
      fileId={fileId}
      existedEvent={existedEvent}
      selectApp={selectApp}
      type={'strategyComponent'}
      getRule={getRule}
      setDataFn={setDataFn}
    />
  );
}
